import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'index.dart';

class VideoPlayerListDemoController extends GetxController {
  VideoPlayerListDemoController();

  final state = VideoPlayerListDemoState();

  /// 在 widget 内存中分配后立即调用。
  @override
  void onInit() {
    super.onInit();
    state.sc = ScrollController()
      ..addListener(() {
        final off = state.sc.position.pixels;
        final l = state.list?.where((e) => (e.off ?? 0) > off);
        if (l == null) return;
        final m = l.lastOrNull;
        if (m?.off != null) {
          if (m!.off! >= off) {
            m.streamController.add(true);
          }
        }
        l.take(l.length - 1).forEach((e) => e.streamController.add(false));
      });
    state.list = [
      VideoModel(
        title: '录视频',
        url: 'http://download.szzfyjzx.icu/IMG_0005.mov',
      ),
      VideoModel(
          title: '实时音频服务',
          url:
              'http://download.szzfyjzx.icu/%E5%BD%95%E5%B1%8F2024-09-24%2011.35.42.mov'),
    ];
  }

  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    super.onReady();
    state.list?.first.streamController.add(true);
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  /// dispose 释放内存
  @override
  void dispose() {
    super.dispose();
  }
}
